package com.example.psysystemhomework;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.psysystemhomework.util.AppManager;

import java.util.Timer;
import java.util.TimerTask;

public class FirstActivity extends AppCompatActivity {
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            View childAt = mFirstLayout.getChildAt(index);
            ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(childAt, "rotationX", 0, 180);
            objectAnimator.setDuration(time);
            objectAnimator.start();
            objectAnimator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animator) {

                }

                @Override
                public void onAnimationEnd(Animator animator) {
                    childAt.setAlpha(0f);
                }

                @Override
                public void onAnimationCancel(Animator animator) {

                }

                @Override
                public void onAnimationRepeat(Animator animator) {

                }
            });

            index++;
            if (index >= childrenSize) {
//                启动
                AppManager appManager = AppManager.getAppManager();
                appManager.addActivity(new MainActivity());
                Intent intent = new Intent(context, appManager.currentActivity().getClass());
                startActivity(intent);
                timer.cancel();
            }

            super.handleMessage(msg);
        }
    };
    private LinearLayout mFirstLayout;
    private int childrenSize;
    private int index;
    private Context context;
    private Timer timer;
    private int time = 300;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        initViews();
    }

    private void initViews() {
        mFirstLayout = findViewById(R.id.first_layout);
        context = this;
        index = 0;
        childrenSize = mFirstLayout.getChildCount();
        startView();
    }

    private void startView() {

        timer = new Timer();

        timer.schedule(new TimerTask() {
            @Override
            public void run() {

                Message message = new Message();
                message.arg1 = index;
                handler.sendMessage(message);


            }
        }, 0, time);

    }
}